<?php

namespace app\common\validate\xilujob;

use app\common\model\xilujob\Area;
use app\common\model\xilujob\CompanyScale;
use think\Db;
use think\Validate;
use function fast\array_except;

class CompanyProfile extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
    ];
    /**
     * 提示消息
     */
    protected $message = [
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => []
    ];

    public function __construct(array $rules = [], array $message = [], array $field = []) {
        $rules['id'] = 'require';
        $rules['company_name'] = 'length:0,50';
        $rules['register_address'] = 'length:0,100';
        $rules['address'] = 'length:0,100';
        $rules['license_name'] = 'length:0,100';
        $rules['category_id'] = 'valid_category';
        $rules['city_id'] = 'valid_city';
        $rules['scale_id'] = 'valid_scale';
        $rules['company_type_id'] = 'valid_company_type';
        $rules['logo'] = 'length:0,255';
        $rules['license'] = 'length:0,500';
        parent::__construct($rules, $message, $field);
        $this->scene['add'] = array_keys($rules);
        $this->scene['edit'] = array_keys(array_except($rules, ['id']));
    }

    protected function valid_category($value,$rule,$data) {
        return Db::name('industry_category')->where('id',$value)
            ->count() > 0;
    }

    protected function valid_city($value,$rule,$data) {
        return Area::where('id', $value)
            ->where('level',2)
            ->count() > 0;
    }

    protected function valid_scale($value,$rule,$data) {
        return CompanyScale::where('id', $value)
                ->count() > 0;
    }

    protected function valid_company_type($value,$rule,$data) {
        return Db::name('company_type')->where('id',$value)
                ->count() > 0;
    }

}
